package com.example.zack.cyrano;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MultiContactsPicker extends Activity  implements View.OnClickListener{
    private ListView listview;
    private Cursor cursor;
    private CursorAdapter adapter;
    Button addButton,cancelButton;
    Set<Integer> checkList = new HashSet<Integer>();
    final static String NAME_COLUMN = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
    final static String PHONE_COLUMN = ContactsContract.CommonDataKinds.Phone.NUMBER;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_list);
        listview=(ListView)findViewById(R.id.listView);
        cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{"_id", NAME_COLUMN, PHONE_COLUMN},
                null, null,  NAME_COLUMN + " ASC");
        adapter = new MyCursorAdapter(this,
                cursor, false);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
                CheckBox check = (CheckBox)view.findViewById(R.id.checkBox1);
                if (checkList.contains(position))
                {
                    checkList.remove(position);
                    check.setChecked(false);
                }
                else
                {
                    checkList.add(position);
                    check.setChecked(true);
                }
            }

        });
        addButton = (Button)findViewById(R.id.button1);
        addButton.setOnClickListener(this);
        cancelButton = (Button)findViewById(R.id.button2);
        cancelButton.setOnClickListener(this);

    }
    class MyCursorAdapter extends CursorAdapter
    {
        private LayoutInflater mInflater;
        private Context mContext;

        public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
            super(context, c, autoRequery);
            mContext = context;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            //String name = cursor.getString(0);
            //String number = cursor.getString(1);
            TextView name = (TextView)view.findViewById(android.R.id.text1);
            name.setText(cursor.getString(1));
            TextView number = (TextView)view.findViewById(android.R.id.text2);
            number.setText(cursor.getString(2));

            CheckBox check = (CheckBox)view.findViewById(R.id.checkBox1);

            if (checkList.contains(cursor.getPosition()))
            {
                check.setChecked(true);
            }
            else
            {
                check.setChecked(false);
            }
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return mInflater.inflate(R.layout.contact_list_item, null);
            //return null;
        }


    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == addButton)
        {
            ArrayList<String> phones = new ArrayList<String>();
            Map<String, String> contacts = new HashMap<String, String>();
            for (int pos : checkList)
            {
                cursor.moveToPosition(pos);
                contacts.put(cursor.getString(2), cursor.getString(1));
                phones.add(cursor.getString(2));
            }
            Intent intent = new Intent();
            intent.putExtra("contacts", (Serializable)contacts);
            intent.putStringArrayListExtra("phones", phones);
            setResult(RESULT_OK, intent);
            finish();
        }
        else if (v == cancelButton)
        {
            setResult(RESULT_CANCELED, null);
            finish();
        }
    }
}
